package cn.yuhh.api.v1.declare;

import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.annotation.NotNull;
import cn.yuhh.core.domain.DeclareInfo;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.Role;
import cn.yuhh.core.type.Status;
import com.magic.spring.mongo.Mongo;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author ysw
 * @create 2020-02-18 15:38
 * @descreption 受理接口
 **/
@Component("accept1.0")
@NotNull(name = {"id"})
public class AcceptService extends AbstractApiService {
    @Override
    public Map service(Map request) {
        DeclareInfo declareInfo = Mongo.buildMongo().eq("_id", new ObjectId(id(request))).findOne(DeclareInfo.class);
        User manager = Mongo.buildMongo().eq("areaInfo.countyCode", declareInfo.getAreaInfo().getCountyCode()).eq("role", Role.DIST_HEADER).eq("status", Status.ACTIVE).findOne(User.class);
        Mongo.buildMongo().eq("_id",new ObjectId(id(request))).updateFirst(update -> {
            update.set("declareStatus", declareInfo.getDeclareStatus().handler(whether(request)));
            update.set("mainManagerId", manager.getId());
            update.set("updateAt", System.currentTimeMillis() / 1000);
            if(!StringUtils.isEmpty(string(request,"reason"))){
                update.set("reason",string(request,"reason"));
                update.set("manager","");
            }
        },DeclareInfo.class);
        declareLog(id(request));
        return ApiResponse.success();
    }
}